home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ShareWare OnLine 2
/
ShareWare OnLine Volume 2 (CMS Software)(1993).iso
/
games2
/
rotise12.zip
/
GETOPT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-09
|
2KB
|
91 lines
/* getopt.c -- A simple implementation of getopt()
1989 Mark Mallett
This is an implementation of getopt(3) for environments that
don't have one. It may not be -- in fact, probably isn't --
fully compatible with the normal getopt(3), but it can be
used in a pinch, which is what this is.
*/
#include <stdio.h>
#include "rotise.h"
char *optarg = NULL; /* Option argument */
int opterr = 1; /* Whether to print errors. */
int optind = 1; /* Current option index */
/*
*//* getopt( argc, argv, optP )
Extract an option from a command line
Accepts :
argc Number of command line arguments
argv Argument vector
optP Ptr to option list, as in getopt(3);
Returns :
<value> EOF if no more options,
or the option character;
*/
int getopt ARGLIST( (argc, argv, optP) )
NFARG( int argc) /* Argument count */
NFARG( char **argv) /* Ptrs to tokens */
FARG( char *optP) /* Ptr to option list */
{
char ch; /* Char */
/* If no more tokens, return EOF */
if ( optind >= argc )
return( EOF );
/* Check first arg for switch (add '/' as switch for IBM'ers) */
if ( argv[optind][0] != '-' && argv[optind][0] != '/' )
return( EOF );
/* Got option, probably; handle it. */
ch = argv[optind][1];
for( ;*optP != '\0'; ++optP ) {
if ( ch == *optP )
break;
if ( optP[1] == ':' )
++optP;
}
if ( *optP == '\0' ) {
/* Unwanted option character. */
if ( opterr )
fprintf( stderr, "%s: illegal option -- %c\n",
( argv[0] != NULL ) && ( *argv[0] != '\0' ) ?
argv[0] : "WRAP", ch );
return( '?' );
}
/* Check for argument wanted */
if ( optP[1] == ':' ) {
if ( argv[optind][2] != NUL )
optarg = &argv[optind][2];
else if ( optind < argc -1 )
optarg = argv[++optind];
else {
if ( opterr )
fprintf( stderr, "%s: option requires an argument -- %c\n",
( argv[0] != NULL ) && ( *argv[0] != '\0' ) ?
argv[0] : "<progname>", ch );
return( '?' );
}
}
else
optarg = NULL;
++optind;
return( ch );
}